﻿CloudDisk.Workbench = {
    category: '',
    header: $('#header'),
	west: $('#nav'),
	body: $('#content'),
	detail: $('#file_info'),
};

(function(w){

    function init(){
        this.header.init();
        this.west.init();
	    this.body.init();

	}
	
	function setPath(cid, fid){
	    this.west.updateContent(cid, fid);
		this.body.setCategory(cid, fid);
	}

    //public methods
	w.init = init;
	w.setPath = setPath;
})(CloudDisk.Workbench);

/* ---------------------------- body ----------------------------------------------*/
(function(w){
    var category = '';
	var el = $("#file_list");
	var fileDetailTpl = $('#fileDetailTpl');
	var selected = [];
	var fileList = new CloudDisk.FileList();
	var detailPanel = CloudDisk.Workbench.detail;
	
	$('#shareBtn,#downloadBtn,#removeBtn').hide();
	
	fileList.onUnselect = fileList.onSelect = function(){
	    var selections = this.getSelections();
		var content = '';
	    if(selections.length > 1){
		    content = '已选定' + selections.length + '项目';
			$('#shareBtn,#downloadBtn,#removeBtn').show();
		} else if(selections.length == 1){
		    content = fileDetailTpl.render(selections[0]);
			$('#shareBtn,#downloadBtn,#removeBtn').show();
		} else {
		    $('#shareBtn,#downloadBtn,#removeBtn').hide();
		}
	    detailPanel.update(content);
	};
	fileList.onOpen = function(data){
		if(data.isFolder){
		    window.location.hash = "cid=" + (category? category: '') + '&fid=' + data.file_id;	
		}
	};
	
		
	$('#uploadBtn').click(function(){
	    //CloudDisk.DdUploader.show();
		CloudDisk.UploadMgr.showUploader();
	});
	$('#addFolderBtn').click(function(){
		fileList.createBlankFile();
	});
	$('#shareBtn').click(function(){
		CloudDisk.ShareWindow.open();
	});
	$('#removeBtn').click(function(){
		var dialog = new CloudDisk.MessageDialog('确认删除？', '删除');
		dialog.appendCommand('确认',  function(){
			alert('OK');
		});
		dialog.appendCommand('取消',  function(){
			alert('Cancel');
		});
		dialog.show();
	});
	
	function init(){
	    this.refresh();
	    //register handler for template change
	    $('#toolbar .thumb').click(function(e){
		    if(e.target.nodeName != 'SPAN' || $(e.target).hasClass('thumb-current')){
			    return false;
			}
		    $(this).children('span').toggleClass('thumb-current');
			if($(e.target).hasClass('thumb-1')){
			    fileList.setView('list');
			} else {
			    fileList.setView('grid');
			}
		});
		
		//select all or unselect all
		$('#file_list .thead .checkbox').click(function(e){
		    if($(this).hasClass('checked')){
			    $(this).removeClass('checked');
				fileList.clearSelections();
				$('#shareBtn,#downloadBtn,#removeBtn').hide();
			} else {
			    $(this).addClass('checked');
				fileList.selectAll();
				$('#shareBtn,#downloadBtn,#removeBtn').show();
			}
		});
		
		//dispatch handler when body clicked
		$('#file_list').click(function(e){
		    //var row = getRow(e.target);
			//$(row).toggleClass('selected');
		});
		
		//show or hide detail info
		$('#detailsBtn').click(function(){	
            var detailPanel = CloudDisk.Workbench.detail;   
			if(!detailPanel.isCollapsed()){
			    detailPanel.collapse(true);
			    $('#file_detail').width($(window).width() - $('#nav').width());
			} else {
			    detailPanel.collapse(false);
			    $('#file_detail').width($(window).width() - $('#file_info').width() - $('#nav').width());
			}
		});
	}
	
	function setCategory(cid, fid){
	    category = cid;
		fileList.render(CloudDisk.MockData.get(cid, fid));
	}
	
    function refresh(){
	    fileList.render(CloudDisk.MockData);
	}
	
	function getRow(o){
	    while(o.nodeName != 'TR'){
		    o = o.parentNode;
		}
		return o;
	}
    
	//public methods
	w.init = init;
	w.refresh = refresh;
	w.setCategory = setCategory;
})(CloudDisk.Workbench.body);

/* ---------------------------- header --------------------------------*/
(function(w){
	
	function init(){
	    $("#username").mouseover(function(){
			CloudDisk.AccountMenu.show(this, {
				    x: $(this).offset().left - 20,
					y: $(this).offset().top + $(this).height()
			});
        });
		
	    this.click(function(e){
		    var category = e.target.getAttribute('category');		    
			if(category){
			    CloudDisk.Util.log('category: ' + e.target.getAttribute('category'));
			    changeLocationHash(category);
			}
		});
	}
    
	//public methods
	w.init = init;
})(CloudDisk.Workbench.header);

/* ---------------------------- west, navigation panel --------------------------------*/
(function(w){
	
	function init(){
	    $("#treeCt").jstree({
		    themes: {
			    dots: false,
				icons: false,
				url: 'theme_01/css/tree.css'
			}
		},{ 
		    "plugins" : ["themes","html_data","ui"] 
		}).bind("click", function(e, data){
			var folderId = e.target.getAttribute('fid');    
			if(folderId){
			    CloudDisk.Util.log('folder: ' + e.target.getAttribute('fid'));
			    changeLocationHash(0, folderId);
			}
        });
		
	    this.click(function(e){
		    var category = e.target.getAttribute('category');		    
			if(category){
			    CloudDisk.Util.log('category: ' + e.target.getAttribute('category'));
			    changeLocationHash(category);
			}
		});
	}
	
	function changeLocationHash(cid, fid){
		var hash = "cid=" + cid;
		if(typeof fid != 'undefined'){
			hash += '&fid=' + fid;
		}
		window.location.hash = hash;
	}
	
    function updateContent(cid, fid){
	    w.find('.nav-first-active').removeClass('nav-first-active');
		$("#treeCt").jstree("deselect_all");

	    $('div[category=' + cid + ']').addClass('nav-first-active');
	    if(cid == '0'){
		    $("#treeCt").jstree("select_node", "#test2"); 
			$("#treeCt").find('li').each(function() { 
			    var li = $(this);
				if(li.find('a')[0].getAttribute('fid') == fid){
				    $("#treeCt").jstree("select_node", li);
					return false;
				}
			});
		}
	}
    
	//public methods
	w.init = init;
	w.updateContent = updateContent;
})(CloudDisk.Workbench.west);


/* ---------------------------- east, detail info panel  --------------------------------*/
(function(w){
    var collapsed = true;
	var tpl = $("#fileDetailTpl");
	
	function init(){
	}
	
    function update(data){
	    this.empty();
	    this.html(tpl.render(data));
	}
	
	function collapse(toHide){
	    if(toHide){
		    this.hide();
			collapsed = true;
		} else {
		    this.show();
			collapsed = false;
		}
	}
	
	function isCollapsed(){
	    return collapsed;
	}
	
	function update(content){
	    this.html(content);
	}
    
	//public methods
	w.init = init;
	w.update = update;
	w.collapse = collapse;
	w.isCollapsed = isCollapsed;
})(CloudDisk.Workbench.detail);